#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/1dsyrk/050613_challenge_124_easy_edge_sorting/

require 'pp'

g = {
}

start = nil

STDIN.readlines.each do |line|
  toks = line.strip.split(/ +/)

  next if toks.size != 3

  edge  = toks[0]
  left  = toks[1]
  right = toks[2]

  if left > right
    tmp   = left
    left  = right
    right = tmp
  end

  g[left] ||= []
  g[left] <<= [edge, right]

  start = left if start.nil? or left < start
end

current = start

order = []

until g[current].nil?
  nextnode = g[current].sort do |a, b|
    a.last <=> b.last
  end.last

  order << nextnode.first
  
  current = nextnode.last
end

pp order
